#!/bin/csh -f

#   This is an example installation script for HotMix.
#
#   The bulk of the code in this script is aimed at determining the
#   INST_PATH -- the place to install the software (this isn't as
#   easy as it might initially seem!).  The default is to copy to an
#   installation directory in /usr/tmp.  If /usr/tmp doesn't exist,
#   there is not enough disk space, or the user prefers another directory,
#   the script will ask for another directory name.  In this case,
#   the script will read the desired directory name, check its validity,
#   check the available disk space, and if everything is OK, will then
#   do the installation.
#
#   To customize the script, you will need to change these 4 parameters:
#
#       MYAPP = name of the application
#
#       INST_DIR = name of the installation directory
#                  (this is the directory name, not the full path name)
#       CD_PATH = directory path of the location of the software
#                 on the HotMix CD-ROM (it must begin with "$HOTMIXDIR",
#                 which is the mount point for the CD-ROM drive).
#       KBYTES_REQ = required size of the full installation (in Kbytes).
#
#   In this simple example, the installation just involves copying
#   the software to the hard disk, and then launching the application.
#   In other cases, the installation will probably be more complicated,
#   so you'll need to change the code at the end of the script as well.
#   The rest of the script should not require modification.
#
#   All of the InstallIt and RemoveIt scripts should reside in the
#   $CD_PATH directory.
#
#                                                 
#
#                                                 


set MYAPP=diver
set INST_DIR=charybda
set KBYTES_REQ=1000

#
#  Test to see if on the new Indy filesystem or old one.
#

set TEST_NEW=`df -k /usr | /usr/bin/awk '{print $7}'`

if ( "$TEST_NEW" == "Mounted /" ) then
   set FILE_STRUCTURE=new

else
   set FILE_STRUCTURE=old
endif


#
#  Determine the INST_PATH name.
#
#  First try /usr/tmp;  then if necessary, ask for another directory.
#

@ MBYTES_REQ = ($KBYTES_REQ) / 1000

if ( $FILE_STRUCTURE == "new" ) then
   set KBYTES_AVAIL=`df -k / | /bin/grep / | /usr/bin/awk '{print $5}'`
else
   set KBYTES_AVAIL=`df -k /usr | /bin/grep /usr | /usr/bin/awk '{print $5}'`
endif

if ( -x /usr/tmp/charybda/diver ) then
	echo " "
	echo "It seems the demo is already installed.  Would you like to launch it? (y/n)?  \c"
	set ans3=($<)
	if ( $ans3 == 'y' ) then
		echo "Launching Charybda Diver ...."

if !($?home) then
        if !($?user) then
                set usermatch = "^[^:]*:[^:]*:`id -u`:"
                echo "user not set... using $usermatch"
        else
                set usermatch = "^$user:"
        endif

        set newhome = `grep "$usermatch" /etc/passwd | head -1 | cut -d: -f6`
        echo "home directory not set... setting to $newhome"
        set home = "$newhome"
endif

		jot -fv $TMPFILEDIR/charybda/diver_demo_instr &

		$TMPFILEDIR/charybda/diver
		exit
	else
		echo " "
		echo "Would you like to continue with another install? (y/n)?  \c"
                set ans3=($<)
                if ( $ans3 != 'y' ) then
                    echo " "
                    echo "... Bye."
                    sleep 3
                    exit
		else
			echo " "
			echo "Would you like to remove this software before contining? (y/n)?  \c"
			set ans3=($<)
			if ( $ans3 == 'y' ) then
				if ( -w /usr/tmp/charybda ) then
					echo " "
					echo "Removing /usr/tmp/charybda "
					rm -rf /usr/tmp/charybda
				else
					echo " "
					echo "Don't have permission! "
					echo "Continuing with install..."
				endif
			endif
                endif
	endif
endif

if ( -w /usr/tmp ) then

    if ( $KBYTES_AVAIL > $KBYTES_REQ ) then

        set INST_PATH=/usr/tmp/$INST_DIR

        echo " "
        echo "This demo requires $MBYTES_REQ Mbytes of disk space."
        echo " "
        echo "It will be installed in a directory named $INST_PATH"
        echo "Is this OK (y/n)?  \c"
        set ans=($<)
        if ( $ans != 'y' ) then
            echo " "
            echo "Do you want to install in another directory (y/n)?  \c"
            set ans2=($<)
            if ( $ans2 != 'y' ) then
                echo " "
                echo "... Bye."
                sleep 3
                exit
            else
                set INST_PATH=none
            endif
        else
            if ( -d $INST_PATH ) then
                echo " "
                echo "It seems the demo is already installed.  Continue anyway (y/n)?  \c"
                set ans3=($<)
                if ( $ans3 != 'y' ) then
                    echo " "
                    echo "... Bye."
                    sleep 3
                    exit
                endif
                rm -rf $INST_PATH
            endif
        endif
    else
        set INST_PATH=none
    endif
else
    set INST_PATH=none
endif

if ( $INST_PATH == "none" ) then

#   Read name of installation directory.

    echo " "
    echo "This demo requires $MBYTES_REQ Mbytes of disk space."

    LOOP1:

    unset INSTDIR_FILESYS

    echo " "
    echo "Enter the full path name of an existing directory"
    echo "in which to copy the software (or enter 'q' to quit):  \c"
    set ans=($<)

#   Check validity of the path name.  If it contains only a "/",
#   this will cause a divide by zero, and fail.
    set TEST1=`echo $ans | cut -f2 -d"/"`
    if ( ! $#TEST1 ) then
            echo " "
            echo "You cannot use directory /.  Please try again."
            goto LOOP1
    endif

    if ( $ans == 'q' ) then
        echo " "
        echo "... Bye."
        sleep 3
        exit
    else if ( $#ans == 0 ) then
        echo " "
        echo "Input error.  Please try again."
        goto LOOP1
    else

#       Check validity of the path name again.  It must begin with "/".
        set TEST2=`echo $ans | cut -f2 -d"/" -s`
        if ( ! $#TEST2 ) then
                echo " "
                echo "The pathname must begin with /.  Please try again."
                goto LOOP1
        endif

        if ( -d $ans ) then

            echo " "
            echo "A $INST_DIR directory will be created in $ans."
            echo "Is this OK (y/n)?:  \c"
            set ans2=($<)
            if ( $ans2 != 'y' ) goto LOOP1

#           Determine the space available for the filesystem
#           containing the requested directory.
#

#                       Check to see if the directory is actually a link or an
#                       nfs mount and do appropriate checks.

                        if ( -l $ans ) then
                                cd $ans
                                set CDIR=`pwd`
                                set KBYTES_AVAIL=`df -k $CDIR | /usr/bin/awk '{print $5}'`
                        endif

                        touch $ans/hmtest
                        if ( ! -r $ans/hmtest ) then

                                echo "Can not write to $ans, please choose another directory."
                                goto LOOP1
                        endif
			/bin/rm -f $ans/hmtest



#           First, find all the mounted filesystems.
            set FILESYSTEMS=`df -k | awk '{print $7}' | grep /`

#           Now, beginning with the full path name of the
#           requested directory, start stripping off the trailing
#           subdirectory names, and compare this to the existing
#           file system names.  If these match exactly, then we've
#           found the right file system.

            set TEST3=`echo $ans | cut -f3 -d"/"`
            if ( ! $#TEST3 ) then
                set PATH=$ans
            else
                set PATH=`dirname $ans`
            endif
            LOOP2:
                foreach FILESYSTEM ($FILESYSTEMS)

#                   Don't do the test if $FILESYSTEM == "/"
                    set TEST4=`echo $FILESYSTEM | cut -f2 -d"/"`
                    if ( $#TEST4 != 0 ) then

                        if ( $PATH == $FILESYSTEM ) then
                            set INSTDIR_FILESYS=$PATH
                        endif
                    endif
                end
                set PATH=`dirname $PATH`

#           Continue looping until we've stripped the pathname
#           down to only "/", or until we find the file system.

            set LOOPTEST=`echo $PATH | cut -f2 -d"/"`
            if (( $#LOOPTEST != 0 ) && ( ! $?INSTDIR_FILESYS )) goto LOOP2

            if ( $?INSTDIR_FILESYS ) then

#               Determine the available space

                set KBYTES_AVAIL=`df -k $INSTDIR_FILESYS | /bin/grep $INSTDIR_FILESYS | /usr/bin/awk '{print $5}'`

            else
                if ( $FILE_STRUCTURE == "new" ) then
                  set KBYTES_AVAIL=`df -k / | /bin/grep / | /usr/bin/awk '{print $5}'`
                else
                echo " "
                echo "  ERROR:  Can't determine the amount of available disk"
                echo "          space for the directory you requested."
                echo " "
                echo "          Continue anyway (y/n)?  \c"
                set ans3=($<)
                if ( $ans3 == 'y' ) then
                    @ KBYTES_AVAIL = ($KBYTES_REQ) + 1
                else
                    echo " "
                    echo "... Bye."
                    sleep 3
                    exit
                endif
            endif
	   endif

            if ( $KBYTES_AVAIL > $KBYTES_REQ ) then
                if ( -w $ans ) then

#                   Success!!!

                    set INST_PATH=$ans/$INST_DIR
                    echo " "
                else
                    echo " "

                    echo "Can't write to directory $ans."
                    echo "Please try again."
                    goto LOOP1
                endif
            else
                echo " "
                echo "Not enough space available."
                echo "Please try again."
                goto LOOP1
            endif
        else
            echo " "
            echo "Can't find directory $ans."
            goto LOOP1
        endif
    endif

    # Check installation directory.
    if ( -d $INST_PATH ) then
        echo " "
        echo "It seems the demo is already installed.  Continue anyway (y/n)?  \c"
        set ans3=($<)
        if ( $ans3 != 'y' ) then
            echo " "
            echo "... Bye."
            sleep 3
            exit
        endif

        rm -rf $INST_PATH
    endif
endif

#  Store INST_PATH in a file so it can be communicated to the RemoveIt script.
#
#  First, try to put it in /usr/tmp.  If we can't, then put it in /tmp
#  and in the user's home directory.  If put in /tmp, the file will
#  disappear if the machine is rebooted.  If put in the user's home
#  directory, we won't be able to find it if the user trying to remove
#  the software is not the same as the user who installed it.  Either
#  case isn't very good, but it's the best we can do if we can't write
#  to /usr/tmp.

#/bin/rm -f /usr/tmp/HOTMIXPATH_$INST_DIR > /dev/null
#/bin/rm -f /tmp/HOTMIXPATH_$INST_DIR > /dev/null

#/bin/rm -f ~/HOTMIXPATH_$INST_DIR > /dev/null

#if ( -w /usr/tmp ) then
#    echo "setenv INST_PATH $INST_PATH" > /usr/tmp/HOTMIXPATH_$INST_DIR
#    /bin/chmod 666 /usr/tmp/HOTMIXPATH_$INST_DIR
#else
#    echo "setenv INST_PATH $INST_PATH" > /tmp/HOTMIXPATH_$INST_DIR
#    /bin/chmod 666 /tmp/HOTMIXPATH_$INST_DIR

#    echo "setenv INST_PATH $INST_PATH" > ~/HOTMIXPATH_$INST_DIR
#    /bin/chmod 666 ~/HOTMIXPATH_$INST_DIR
#endif

#
#-----------------------------------------------------------------------
#
#  Now that we've finally determined INST_PATH, do the installation.
#  In this case, just copy the software and execute the application.

echo " "
echo "Installing Diver Demo.  Please wait..."
	 mkdir -p $INST_PATH
# 	cd $INST_PATH
	cp -r $CD_PATH/charybda/* $INST_PATH
sleep 10
echo " "
echo "Done."
echo " "
echo "To run the demo:" 
echo "Move to the $INST_PATH directory"
echo "and execute $MYAPP."
echo " "
echo " "
echo "You should also view the demo instructions"
echo "in the $INST_PATH directory. "
echo " "
echo "Do you want to run the demo now (y/n)?  \c"
set ans=($<)
if ( $ans == 'y' ) then
	cd $INST_PATH
	./diver &
	jot -fv ./diver_demo_instr
else
    echo "... Bye."
    sleep 3
endif
echo "Press Enter to exit this window...  \c"
set a = $<

